package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPauseOrResetEngineeringOrderView;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringPublicationOrderHasBeenReceivedActicity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringSurveyOrderHasBeenReceivedActicity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceiveMaintenanceOrderActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ViewEngineeringConstructOrderDetailActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ViewEngineeringInstallOrderDetailActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.WattingToReceiveEngineeringOrderAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PauseOrResetEngineeringOrderPresenter;
import com.cloudsigns.factorymanager2.event.ReceiveEngineringOrderListEvent;
import com.cloudsigns.factorymanager2.project.activity.BatchInstallationOrderDetailActivity;
import com.cloudsigns.factorymanager2.project.activity.BatchInstallationOrderDetailActivity1;
import com.cloudsigns.factorymanager2.project.activity.EngineeringConstructionOrderDetailActivity;
import com.cloudsigns.factorymanager2.project.activity.EngineeringInstallationOrderDetailActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 已经接收
 * Created by tony on 2018/1/14.
 */

public class HasBeenReceivedFragment extends Fragment implements View.OnClickListener, WattingToReceiveEngineeringOrderAdapter.CheckedChangeListener, IPauseOrResetEngineeringOrderView {
    private final String TAG = "HasBeenReceivedFragment";
    private ViewGroup ll_btn;
    private TextView btn_left, btn_right;
    private ListView lv;
    private WattingToReceiveEngineeringOrderAdapter adapter;
    private List<OrderParentBean> dataList = new ArrayList<>();
    private boolean LEFT_CLICKABLE = false, RIGHT_CLICKABLE = false;
    private PauseOrResetEngineeringOrderPresenter pauseOrResetEngineeringOrderPresenter;
    private boolean hasPauseOrder;
    private int selectedPositon;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_watting_to_receive_engineering_order, null);
        ll_btn = view.findViewById(R.id.ll_btn);
       // ll_btn.setVisibility(View.VISIBLE);
        btn_left = view.findViewById(R.id.btn_left);
        btn_right = view.findViewById(R.id.btn_right);

        lv = view.findViewById(R.id.lv);


        btn_left.setOnClickListener(this);
        btn_right.setOnClickListener(this);


        adapter = new WattingToReceiveEngineeringOrderAdapter(getActivity(), TAG);
        adapter.setData(dataList);
        adapter.setCheckedChangeListener(this);
        lv.setAdapter(adapter);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {


                Intent mIntent;
                OrderParentBean order = dataList.get(position);

//                CommonEnum.EngineeringOrderStatus status = order.getStatus();

//                if (null != status && status == CommonEnum.EngineeringOrderStatus.PAUSE) {
//                    Toast.makeText(getActivity(), "该工程已暂停!", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                if (null != order.getCategory()) {
                    CommonEnum.EngineeringORderCategory category = order.getCategory();
                    // 工程安装
                    if (category == CommonEnum.EngineeringORderCategory.INSTALL) {
                        mIntent = new Intent(getActivity(), ViewEngineeringInstallOrderDetailActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, order);
                        startActivity(mIntent);
                    }
                    // 批量安装
                    else if (category == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                       // mIntent = new Intent(getActivity(), BatchInstallationOrderDetailActivity.class);
                        mIntent = new Intent(getActivity(), BatchInstallationOrderDetailActivity1.class);
                        startActivity(mIntent);
                    }
                    // 工程施工
                    else if (category == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                        mIntent = new Intent(getActivity(), ViewEngineeringConstructOrderDetailActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, order);
                        startActivity(mIntent);
                    }
                    // 维修
                    else if (category == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                        mIntent = new Intent(getActivity(), ReceiveMaintenanceOrderActivity.class);
                        mIntent.putExtra("type","HasBeenReceivedFragment");
                        startActivity(mIntent);
                    }
                    // 现场勘查
                    else if (category == CommonEnum.EngineeringORderCategory.SURVEY) {
                        mIntent = new Intent(getActivity(), EngineeringSurveyOrderHasBeenReceivedActicity.class);
                        startActivity(mIntent);
                    }
                    // 广告上刊
                    else if (category == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                        mIntent = new Intent(getActivity(), EngineeringPublicationOrderHasBeenReceivedActicity.class);
                        startActivity(mIntent);
                    }
                }
            }
        });

        pauseOrResetEngineeringOrderPresenter = new PauseOrResetEngineeringOrderPresenter(this);
        return view;
    }

    private void pause() {
        btnClickable(false, false);

        hasPauseOrder = true;
        pauseOrResetEngineeringOrderPresenter.pauseOrResetEngineeringOrder();


    }

    private void reset() {
        btnClickable(false, false);

        hasPauseOrder = false;
        pauseOrResetEngineeringOrderPresenter.pauseOrResetEngineeringOrder();


    }

    private void btnClickable(boolean leftbool, boolean rightbool) {
        LEFT_CLICKABLE = leftbool;
        RIGHT_CLICKABLE = rightbool;

        if (leftbool) {
            btn_left.setClickable(true);
            btn_left.setBackgroundResource(R.mipmap.btn_shor_bg_blue);
        } else {
            btn_left.setClickable(false);
            btn_left.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }

        if (rightbool) {
            btn_right.setClickable(true);
            btn_right.setBackgroundResource(R.mipmap.btn_shor_bg_red);
        } else {
            btn_right.setClickable(false);
            btn_right.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_left:  //暂停
                if (LEFT_CLICKABLE)
                    pause();
                break;
            case R.id.btn_right:  //恢复
                if (RIGHT_CLICKABLE)
                    reset();
                break;

        }
    }

    @Override
    public void checkedChange() {
        selectedPositon = adapter.getSelectedPosition();
        if (selectedPositon == -1) {
            btnClickable(false, false);
        } else {
            if (dataList.get(selectedPositon).isHasPauseOrder())
                btnClickable(false, true);
            else
                btnClickable(true, false);
        }


    }


    public void initData(List<OrderParentBean> engineeringOrderBeanList) {
        if (engineeringOrderBeanList != null) {
            dataList.clear();
            dataList.addAll(engineeringOrderBeanList);
            adapter.setData(dataList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean getHasPauseOrder() {
        return hasPauseOrder;
    }

    @Override
    public CommonEnum.EngineeringORderCategory getType() {
        if (selectedPositon >= 0)
            return dataList.get(selectedPositon).getCategory();

        return null;
    }

    @Override
    public String getOrderId() {
        if (selectedPositon >= 0)
            return dataList.get(selectedPositon).getId();

        return null;
    }

    @Override
    public void pauseOrResetOrderSuccessed() {
        if (hasPauseOrder) {
            Toast.makeText(getActivity(), "暂停成功", Toast.LENGTH_SHORT).show();
            adapter.setSelectedPosition(-1);

        } else {
            Toast.makeText(getActivity(), "恢复成功", Toast.LENGTH_SHORT).show();
            adapter.setSelectedPosition(-1);

        }

        EventBus.getDefault().post(new ReceiveEngineringOrderListEvent());
    }

    @Override
    public void pauseOrResetOrderFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
        if (hasPauseOrder) {
            hasPauseOrder = false;
            btnClickable(true, false);
        } else {
            hasPauseOrder = true;
            btnClickable(false, true);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
